package com.njupt.yanglonglong.report.demo.utils.websocket;

import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * @className: WSTools
 * @Description: websocket
 * @author: lpl
 * @date: 2021/5/17 16:51
 */
public class WsTools {
    public static ByteBuffer makeSendText(BaseData data) {
        ByteBuffer buffer = ByteBuffer.allocate(BaseData.DATACLASS_LENGTH);
        buffer.put((byte) (((data.getCtrl() << 4)) | data.getType()));
        byte[] temp = new byte[3];
        System.arraycopy(ByteArrayConveter.getByteArray(data.getLength_total()), 1, temp, 0, 3);
        buffer.put(temp);
        buffer.put(ByteArrayConveter.getByteArray(data.getNumber()));
        buffer.put(ByteArrayConveter.getByteArray(data.getLength_data()));
        buffer.put(data.getData());
        buffer.position(0);
        buffer.limit(BaseData.DATACLASS_LENGTH);
        return buffer;
    }

    public static BaseData takeSendText(byte[] message) {
        BaseData Base = new BaseData();
        Base.setCtrl((byte) (message[0] >> 4));
        Base.setType((byte) (message[0] & 0x0f));
        int temp = ((message[1] << 16) | ByteArrayConveter.getShort(message, 2));
        Base.setLength_total(temp);
        Base.setNumber(ByteArrayConveter.getShort(message, 4));
        Base.setLength_data(ByteArrayConveter.getShort(message, 6));
        byte[] t = new byte[BaseData.DATACLASS_LENGTH - BaseData.HEADER_LENGTH];
        System.arraycopy(message, 8, t, 0, t.length);
        Base.setData(t);
        return Base;
    }

    public static void main(String[] args) {
        byte[] a = new byte[BaseData.DATACLASS_LENGTH];
        for (int i = 0; i < BaseData.DATACLASS_LENGTH; i++) {
            a[i] = (byte) (i & 0xff);
        }
        BaseData Base = takeSendText(a);
        System.out.println(Base);
        Base.setCtrl((byte) 1);
        Base.setType((byte) 3);
        Base.setLength_total(0x010203);
        Base.setNumber((short) 0x01);
        Base.setLength_data((short) 0x14);
        byte[] b = new byte[BaseData.DATACLASS_LENGTH - BaseData.HEADER_LENGTH];
        System.arraycopy(a, 0, b, 0, BaseData.DATACLASS_LENGTH - BaseData.HEADER_LENGTH);
        System.out.println(Arrays.toString(b));
        Base.setData(b);
        ByteBuffer buffer = makeSendText(Base);
        buffer.get(a, 0, BaseData.DATACLASS_LENGTH);
        System.out.println(Arrays.toString(a));
    }
}

class ByteArrayConveter {

    // char转换为byte[2]数组
    public static byte[] getByteArray(char c) {
        byte[] b = new byte[2];
        b[0] = (byte) ((c & 0xff00) >> 8);
        b[1] = (byte) (c & 0x00ff);
        return b;
    }

    // 从byte数组的index处的连续两个字节获得一个char
    public static char getChar(byte[] arr, int index) {
        return (char) (0xff00 & arr[index] << 8 | (0xff & arr[index + 1]));
    }

    // short转换为byte[2]数组
    public static byte[] getByteArray(short s) {
        byte[] b = new byte[2];
        b[0] = (byte) ((s & 0xff00) >> 8);
        b[1] = (byte) (s & 0x00ff);
        return b;
    }

    // 从byte数组的index处的连续两个字节获得一个short
    public static short getShort(byte[] arr, int index) {
        return (short) (0xff00 & arr[index] << 8 | (0xff & arr[index + 1]));
    }

    // int转换为byte[4]数组
    public static byte[] getByteArray(int i) {
        byte[] b = new byte[4];
        b[0] = (byte) ((i & 0xff000000) >> 24);
        b[1] = (byte) ((i & 0x00ff0000) >> 16);
        b[2] = (byte) ((i & 0x0000ff00) >> 8);
        b[3] = (byte) (i & 0x000000ff);
        return b;
    }

    // 从byte数组的index处的连续4个字节获得一个int
    public static int getInt(byte[] arr, int index) {
        return (0xff000000 & (arr[index + 0] << 24)) |
                (0x00ff0000 & (arr[index + 1] << 16)) |
                (0x0000ff00 & (arr[index + 2] << 8)) |
                (0x000000ff & arr[index + 3]);
    }

    // float转换为byte[4]数组
    public static byte[] getByteArray(float f) {
        int intbits = Float.floatToIntBits(f);//将float里面的二进制串解释为int整数
        return getByteArray(intbits);
    }

    // 从byte数组的index处的连续4个字节获得一个float
    public static float getFloat(byte[] arr, int index) {
        return Float.intBitsToFloat(getInt(arr, index));
    }

    // long转换为byte[8]数组
    public static byte[] getByteArray(long l) {
        byte b[] = new byte[8];
        b[0] = (byte) (0xff & (l >> 56));
        b[1] = (byte) (0xff & (l >> 48));
        b[2] = (byte) (0xff & (l >> 40));
        b[3] = (byte) (0xff & (l >> 32));
        b[4] = (byte) (0xff & (l >> 24));
        b[5] = (byte) (0xff & (l >> 16));
        b[6] = (byte) (0xff & (l >> 8));
        b[7] = (byte) (0xff & l);
        return b;
    }

    // 从byte数组的index处的连续8个字节获得一个long
    public static long getLong(byte[] arr, int index) {
        return (0xff00000000000000L & ((long) arr[index + 0] << 56)) |
                (0x00ff000000000000L & ((long) arr[index + 1] << 48)) |
                (0x0000ff0000000000L & ((long) arr[index + 2] << 40)) |
                (0x000000ff00000000L & ((long) arr[index + 3] << 32)) |
                (0x00000000ff000000L & ((long) arr[index + 4] << 24)) |
                (0x0000000000ff0000L & ((long) arr[index + 5] << 16)) |
                (0x000000000000ff00L & ((long) arr[index + 6] << 8)) |
                (0x00000000000000ffL & (long) arr[index + 7]);
    }

    // double转换为byte[8]数组
    public static byte[] getByteArray(double d) {
        long longbits = Double.doubleToLongBits(d);
        return getByteArray(longbits);
    }

    // 从byte数组的index处的连续8个字节获得一个double
    public static double getDouble(byte[] arr, int index) {
        return Double.longBitsToDouble(getLong(arr, index));
    }

    public static void main(String[] args) {


    }
}

